// subpage/pages/order/order.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailUserInfo:'',
    index: 0,
    multiIndex: [0, 0, 0],
    startTime:'',
    endTime:'',
    isExistsOrder:false,
    chargeOrder:[],
    date: '2021',
    show: false,
    isShowFirst: 1,
    active: true,
    orderState:[
      {'id':1,'label':'充电中'},
      {'id':2,'label':'充电完成'},
      {'id':3,'label':'充电异常'},
      {'id':4,'label':'充电失败'},
    ],
    currentNav: 1,
    isRefresh: false,
    totalPage: 0,
    page: 1,
    isShowBottom: false,
    hasNext:false,
    isRequest:true,
    ordertype:false
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isShowFirst : options.state
    })
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("下拉刷新", e)
    if (!this.data.isRefresh) {
      //如果总页数 小于等于 当前页数 提示无更多数据
      if (this.data.hasNext) {
        this.setData({
          page: this.data.page + 1,
          isRefresh:true
        })
        this.getOrderList()
      } else{
        console.log('没有更多')
        this.setData({
          isShowBottom: true
        })
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeTab(e) {
   console.log('eeeeeeeeeeeeee',this.data.ordertype)
    if(!this.data.isRequest){
      this.setData({
        isShowFirst: e.target.dataset.id,
        chargeOrder: [],
        ordertype:false,
        isRequest:true
      })
      this.getOrderList()
    }
  },
  orderDetail(e) {
    console.log('传输',e)
    wx.navigateTo({
      url: '/subpage/pages/detail/detail?oid='+e.target.dataset.oid,
    })
  },
  getOrderList() {
    let that = this
    if(that.data.isRequest === false){
      return
    }
    service("POST","/order/list",{
      page: this.data.page,
      state: this.data.isShowFirst
    }).then(res => {
      console.log('订单列表', res)
      if(res.data.code ==0){
        let data = res.data.page.list
        
        data.map(item => {
          item.minintus = 0
          if(item.beginTime){
            if(item.endTime){
             item.minintus = Math.abs(new Date(item.endTime) -new Date(item.beginTime))/1000/60
            }else{
              item.minintus =Math.abs(new Date() -new Date(item.beginTime))/1000/60
            }
          }
          return item
        })
        that.setData({
          chargeOrder: [...that.data.chargeOrder,...res.data.page.list],
          isRefresh: false,
          hasNext:res.data.page.hasNext,
          isRequest:false
        })
      }
      
    })
  },
  //复制
  copy: function (e) {
    console.log('复制',e)
    wx.setClipboardData({
      data: e.target.dataset.oid,
    });
    // wx.showToast({
    //   title: '复制成功',
    // })
  }
})